<?php

/**
 * Von dieser Klasse leiten Navigationselemente ab.
 *
 * Bsp: Sitemap
 *
 */
abstract class WSB_Xtension_Navigation extends WSB_Xtension_Base implements WSB_Xtension_Interface
{

	/**
	 * @var array
	 */
	static protected $_rootline = array();

	/**
	 * @var Zend_Config
	 */
	protected $_config = null;

	/**
	 * @var integer
	 */
	protected $_id = 0;

	/**
	 * @var array
	 */
	protected $_levelEntry = array();

	/**
	 * @var integer
	 */
	protected $_branchId = 0;

	/**
	 * @return void
	 */
	public function init()
	{
		parent::init();
		$this->_id = WSB_Frontend::getInstance()->getPageId();
		$this->setRootline();
		$config = $this->getConfiguration();
		if($config instanceof Zend_Config) {
			$this->_config = $config;
			$this->_levelEntry = intval($config->_levelEntry);
			$this->_branchId = intval($config->branchId);
		}
	}

    /**
     * Definiert basierend auf der aktuellen ID die "Rootlinie" und speichert
     * diese im Array self::$_rootline
     *
     * @return void
     */
    private function setRootline()
	{
		if(empty(self::$_rootline) && !empty($this->_id)) {
			$nodePage = new WSB_Node_Page();
			$branch = $nodePage->getBranch($this->_id);
			if($branch instanceof WSB_Node_Page) {
				while(!$branch->isRoot()) {
					array_push(self::$_rootline, $branch->getPageId());
					$branch = $branch->getParent();
				}
				array_push(self::$_rootline, $branch->getPageId());
			}
		}
	}

	/**
	 * @param integer $id
	 * @return boolean
	 */
	protected function _inRootline($id)
	{
		if(in_array($id, self::$_rootline)) {
			return true;
		}
		return false;
	}

	/**
	 * @param string $str
	 * @param string $wrapper
	 * @return string
	 */
	protected function _wrap($str, $wrapper)
	{
		if(empty($str)) {
			return '';
		}
		if($wrapper != '') {
			return sprintf($wrapper, $str);
		}
		return $str;
	}

}
